package com.army.study.slice;

import com.army.study.ResourceTable;
import com.army.study.database.DatabaseUtils;
import com.army.study.database.FormDatabase;
import com.army.study.database.Sensor;
import com.army.study.common.PermissionBridge;
import com.army.study.service.StepSensorService;
import com.army.study.utils.DateUtils;
import com.army.study.utils.LogUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.ProgressBar;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.orm.OrmContext;

public class MainAbilitySlice extends AbilitySlice implements PermissionBridge.OnPermissionStateListener {
    private static ChangeUI cs;
    private Text step;
    private ProgressBar progressBar;
    private int value;
    private DatabaseHelper helper = new DatabaseHelper(this);
    private OrmContext connect;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_form_router_page);
        new PermissionBridge().setOnPermissionStateListener(this);
        initComponent();
        cs = new ChangeUI();
    }

    private void initComponent() {
        step = (Text) findComponentById(ResourceTable.Id_step);
        connect = helper.getOrmContext("FormDatabase", "FormDatabase.db", FormDatabase.class);
        Sensor sensor = DatabaseUtils.getSensorData(connect, DateUtils.getDate(0));
        if (sensor != null) {
            value = sensor.getStepsValue();
        }
        step.setText(value + "");
        progressBar = (ProgressBar)findComponentById(ResourceTable.Id_progressBar);
        progressBar.setProgressValue(value);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onPermissionGranted() {
        LogUtils.debug("****SimpleName", StepSensorService.class.getSimpleName());
        LogUtils.debug("****Name", StepSensorService.class.getName());
        Intent intentService = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(getBundleName())
                .withAbilityName(StepSensorService.class.getName())
                .build();

        intentService.setOperation(operation);
        startAbility(intentService);
    }

    @Override
    public void onPermissionDenied() {
        showTips(MainAbilitySlice.this, "No Permission");
    }

    private void showTips(Context context, String message) {
        getUITaskDispatcher().asyncDispatch(() -> {
            ToastDialog toastDialog = new ToastDialog(context);
            toastDialog.setAutoClosable(false);
            toastDialog.setContentText(message);
            toastDialog.show();
        });
    }


    public static void updatePage(String value) {
        cs.updatePage(value);
    }

    /**
     * 修改页面
     */
    private class ChangeUI {
        public void updatePage(String sensorValue) {
            step.setText(sensorValue);
            progressBar.setProgressValue(Integer.parseInt(sensorValue));
        }
    }
}
